package com.qzxy.premiumhotelsystem.interceptors;

import cn.hutool.jwt.Claims;
import com.qzxy.premiumhotelsystem.utils.BaseContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;


/**
 * ClassName: TestInterceptor
 * Package: com.qzxy.premiumhotelsystem.interceptors
 * Description:  测试用的，为了取用户id
 *
 * @Author Qwh
 * @Create 2024/9/14 20:05
 * @Version 1.0
 */

@Component
public class TestInterceptor implements HandlerInterceptor {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断当前拦截到的是Controller的方法还是其他资源!!!
        if (!(handler instanceof HandlerMethod)) {
            //当前拦截到的不是动态方法，直接放行
            return true;
        }

        BaseContext.setCurrentId(1);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //清理ThreadLocal数据，以防出现内存泄露
        BaseContext.removeCurrentId();
    }
}

